#! /usr/local/bin/python
#-*- coding: utf-8 -*-

"""Représente un jeu en mémoire.

Une partie est caractérisée par un
nombre de lignes, de colonnes et de mines.
"""

"""
Minesweeper 3K. Original implementation of the classic game of mines
for Python 3.
Copyright (C) 2010  Cedric Bonhomme

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Minesweeper 3K  Copyright (C) 2010  Cedric Bonhomme
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
"""

__author__ = "Jerome Hussenet, Cedric Bonhomme"
__date__ = "$Date: 2010/02/27 $"
__copyright__ = "Copyright (c) 2010 Jerome Hussenet, Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPL 3"

import random

class grille(object):
    """Classe Grille

    Cette classe représente un jeu en mémoire (mines, colonnes, lignes.).
    """
    def __init__(self, w, h, m):
        """Constructeur.

        Construit une grille de largeur w, de hauteur h et contenant m mines.
        """
        for var in (w, h):
            if var < 10 or var > 40:
                raise ValueError(_("Les nombres de lignes et de colonnes doivent être entre 10 et 40."))
        if m > int(((w * h) * 30 / 100)) or m < int(((w * h) * 10 / 100)):
            raise ValueError(_("Le nombre de mines doit être entre 10% et 30% du nombre de cases."))

        self.c = [] # liste contenant l'emplacement des mines
        self.largeur = w
        self.hauteur = h
        self.nbmines = m
        for i in range(h):
            l = []
            for i in range(w):
                l.append('0')
            self.c.append(l)
        for i in range(m):
            occupe = 0
            while occupe == 0:
                wr = random.randint(0, w-1)
                hr = random.randint(0, h-1)
                if self.c[hr][wr] == '0':
                    occupe = 1
                    self.c[hr][wr] = 'm'
        print(self)

    def chargerGrille(self, w, h, m, l):
        """Reinitilalise les variables de la grille (largeur, hauteur, nombre de mines).

        Fonction appelée lors du rechargement d'une partie sauvegardée.
        """
        self.largeur = w
        self.hauteur = h
        self.nbmines = m
        self.c = l

    def getVoisinage(self, c, l):
        """Retourne la liste des cases voisines de (l,c).
        """
        r = [(c-1,l), (c-1,l+1), (c,l+1), (c+1,l+1), (c+1,l), (c+1,l-1), (c,l-1), (c-1,l-1)]
        asup = []
        for i in r:
            if i[0]<0 or i[1]<0 or i[1]>(self.largeur-1) or i[0]>(self.hauteur-1):
                asup.append(i)
        for i in asup:
            r.remove(i)
        return r

    def getNbMinesVois(self, l, c):
        """Retourne le nombre de mines voisines de (l,c).
        """
        r = 0
        for i in self.getVoisinage(l,c):
            if self.c[i[0]][i[1]] == 'm':
                r += 1
        return r

    def __str__(self):
        """Affichage console du graphe.
        """
        s = '#' + '#' * self.largeur + '#\n'
        for i in self.c:
            s = s + '#'
            for j in i:
                if j == '0':
                    s = s +' '
                else:
                    s = s + str(j)
            s = s + '#\n'
        s = s + '#' + '#' * self.largeur + '#\n'
        return s